using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ShopForWeb.Api.Utils.Extensions;
using ShopForWeb.Dtos.ViewModel;
using ShopForWeb.Services;

namespace ShopForWeb.Api.Controllers.Admin
{
  [ApiController]
  [Route("api/admin/[controller]")]
  public class ProductController : ControllerBase
  {
    private ProductServices _productService;
    private IWebHostEnvironment _host;
    public ProductController(ProductServices productServices, IWebHostEnvironment host)
    {
      this._productService = productServices;
      this._host = host;
    }
    [HttpPost]
    public async Task<ResponseView> AddNewAsync([FromBody] ProductInfo product)
    {
      product.Photo = this.ImgSave(product.Base64Str, product.Photo, _host.WebRootPath);
      int result = await _productService.AddNewAsync(product);
      if (result == 0)
        return new ResponseView()
        {
          Code = 500,
          Message = "商品数据添加失败"
        };
      else
        return new ResponseView()
        {
          Code = 200,
          Message = "商品数据添加成功"
        };
    }
    [HttpDelete("{productId}")]
    public async Task<ResponseView> RemoveAsync([FromRoute] int productId, [FromQuery] string photo)
    {
      int result = await _productService.DeleteAsync(productId);
      if (result == 0)
        return new ResponseView()
        {
          Code = 500,
          Message = "商品删除失败"
        };
      else
      {
        this.ImgDel(photo, _host.WebRootPath);
        return new ResponseView()
        {
          Code = 200,
          Message = "商品删除成功"
        };
      }
    }
  }
}
